﻿using System;
using System.Web.Http;
using FikriKibar.Business.Abstract;
using FikriKibar.Business.Concrete;
using FikriKibar.Common.Exceptions;
using FikriKibar.Entities.Composite.UserModels;

namespace FikriKibar.UI.WebApi.Controllers
{
    public class UserController : ApiController
    {
        public ISessionService SessionManager => new SessionManager();
        public ILanguageService LanguageManager => new LanguageManager();

        [HttpGet]
        public IHttpActionResult Login()
        {
            return Ok(SessionManager.Login());
        }
        [HttpPost]
        public IHttpActionResult Login(LoginModel model)
        {
            try
            {
                SessionManager.Login(model);
            }
            catch (ValidationException exception)
            {
                var validationResult = exception.ValidationResult;
                foreach (var t in validationResult)
                {
                    ModelState.AddModelError(t.PropertyName, t.ErrorMessage);
                }
            }

            catch (Exception exception)
            {
                model.Message = exception.Message;
            }

            finally
            {
                model.LanguageList = LanguageManager.GetAll();
            }
            return Ok(model);
        }
    }
}
